package com.filesearcher;

import java.io.IOException;
import java.util.Map;

public class DecompressThread extends Thread 
{
	private byte[] decompressData;
	private ResultsTab resultsTab;
	private Map tabIndexes;
	
	public DecompressThread( byte[] data, ResultsTab tab ) 
	{
		decompressData = data;
		resultsTab = tab;
	}
	
	public void run()
	{
		try 
		{
			tabIndexes = GrepSearchUI.getTabIndexes();
			
			int tabIndex = Integer.parseInt( ( String )tabIndexes.get( Integer.toString( resultsTab.hashCode() ) ) );
			
			if ( tabIndex >= 0 )
			{
				ResultsTab tab = ( ResultsTab )GrepSearchUI.tabPane.getComponentAt( tabIndex );
				tab.setCurrentMessageText( "Loading results from disk, may take a moment" );
				tab.setSearchInProgress( true );
					
				String tabDataString = StringCompress.decompress( decompressData );
				
				tabIndexes = GrepSearchUI.getTabIndexes();
				tab = ( ResultsTab )GrepSearchUI.tabPane.getComponentAt( Integer.parseInt( ( String )tabIndexes.get( Integer.toString( resultsTab.hashCode() ) ) ) );
				tab.setText( tabDataString );
				tab.setCurrentMessageText( "Results restored from disk" );
				tab.setSearchInProgress( false );	
			}
						
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
